home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 August: Tool Chest / Dev.CD Aug 00 TC Disk 2.toast / pc / sample code / human interface toolbox / coloredcheckbox / coloredcheckbox.c next >
Encoding:
C/C++ Source or Header  |  2000-06-23  |  3.8 KB  |  163 lines

  1. /*
  2.     File:        ColoredCheckBox.c
  3.  
  4.     Contains:    This app demonstrates how to create a check box on the        
  5.                 gray window backgrounds.The key to this is making sure    
  6.                 that the background color for the window is set.             
  7.                 a 'wctb' was created using ResEdit which provides the         
  8.                 gray content for the window.
  9.  
  10.     Written by: Larry Lai    
  11.  
  12.     Copyright:    Copyright © 1994-1999 by Apple Computer, Inc., All Rights Reserved.
  13.  
  14.                 You may incorporate this Apple sample source code into your program(s) without
  15.                 restriction. This Apple sample source code has been provided "AS IS" and the
  16.                 responsibility for its operation is yours. You are not permitted to redistribute
  17.                 this Apple sample source code as "Apple sample source code" after having made
  18.                 changes. If you're going to re-distribute the source, we require that you make
  19.                 it clear in the source that the code was descended from Apple sample source
  20.                 code, but that you've made changes.
  21.  
  22.     Change History (most recent first):
  23.                 7/19/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  24.                 
  25.  
  26. */
  27.  
  28. #include <Sound.h>
  29. #include <Fonts.h>
  30. #include <Windows.h>
  31. #include <Menus.h>
  32. #include <Events.h>
  33. #include <Resources.h>
  34.  
  35. #define    rColoredCheckBoxWindow         128
  36. #define    rCheckBox                    128
  37. #define    kSoundID                    128
  38.  
  39. Boolean    gQuit;
  40.  
  41. void InitToolbox(void);
  42. void MyCreateColoredCheckBoxWindow();
  43. void DoEventLoop();
  44. void DoContentClick(WindowPtr, EventRecord);
  45. void DoCheckBox(Point, ControlHandle);
  46.  
  47. void InitToolbox()
  48. {
  49.     InitGraf((Ptr) &qd.thePort);
  50.     InitFonts();
  51.     InitWindows();
  52.     InitMenus();
  53.     FlushEvents(everyEvent,0);
  54.     InitDialogs(0L);
  55.     InitCursor();
  56. }
  57.  
  58. void main()
  59. {
  60.     
  61.     InitToolbox();
  62.     
  63.     MyCreateColoredCheckBoxWindow();
  64.  
  65.     DoEventLoop();
  66. }
  67.  
  68. void MyCreateColoredCheckBoxWindow()
  69. {
  70.     WindowPtr        myWindow;
  71.     
  72.     myWindow = GetNewCWindow(rColoredCheckBoxWindow, nil, (WindowPtr) -1);
  73.     if (myWindow != nil)
  74.     {
  75.         SetPort (myWindow);
  76.         GetNewControl(rCheckBox, myWindow);
  77.     }
  78.     else
  79.         SysBeep(10);
  80.         
  81. }
  82.  
  83. void DoEventLoop()
  84. {
  85.     EventRecord anEvent;
  86.     WindowPtr   evtWind;
  87.     short       clickArea;
  88.     Rect        screenRect;
  89.  
  90.     do{
  91.         if (WaitNextEvent( everyEvent, &anEvent, 0, nil ))
  92.         {
  93.             if (anEvent.what == mouseDown)
  94.             {
  95.                 clickArea = FindWindow( anEvent.where, &evtWind );
  96.                 
  97.                 if (clickArea == inDrag)
  98.                 {
  99.                     screenRect = (**GetGrayRgn ()).rgnBBox;
  100.                     DragWindow( evtWind, anEvent.where, &screenRect );
  101.                 }
  102.                 else if (clickArea == inContent)
  103.                 {
  104.                     if (evtWind != FrontWindow())
  105.                         SelectWindow( evtWind );
  106.                     else
  107.                         DoContentClick(evtWind, anEvent);    
  108.                     }
  109.                 else if (clickArea == inGoAway)
  110.                     if (TrackGoAway( evtWind, anEvent.where ))
  111.                         gQuit = true;
  112.             }
  113.             else if (anEvent.what == updateEvt)
  114.             {
  115.                 evtWind = (WindowPtr)anEvent.message;    
  116.                 SetPort( evtWind );
  117.                 
  118.                 BeginUpdate( evtWind );
  119.                 UpdateControls(evtWind, evtWind->visRgn);
  120.                 EndUpdate (evtWind);
  121.             }
  122.         }
  123.     }while (!gQuit);
  124. }
  125.  
  126.     
  127. void DoContentClick(WindowPtr theWindow, EventRecord theEvent)
  128. {
  129.     Point            mouse;
  130.     ControlHandle    control;
  131.     short            part;
  132.     
  133.     SetPort(theWindow);
  134.     mouse = theEvent.where;    /*Get the mouse location*/
  135.     GlobalToLocal(&mouse);    /*convert to local coordinates*/
  136.     part = FindControl(mouse, theWindow, &control);
  137.  
  138.     if(part == kControlCheckBoxPart)
  139.         DoCheckBox(mouse, control);
  140.  
  141. }
  142.  
  143. void DoCheckBox(Point mouse, ControlHandle control)
  144. {
  145.     short     checkbox;
  146.     Handle    theSound;
  147.  
  148.     if (TrackControl(control, mouse, nil))        /*user clicks checkbox*/
  149.     {
  150.         checkbox = GetControlValue(control);        /*get last value of checkbox*/
  151.         checkbox = 1- checkbox;                    /*toggle value of checkbox*/
  152.         SetControlValue(control, checkbox);            /*set checkbox to new value*/
  153.         if(checkbox == 1)                        /*express yourself next time user clicks the checkbox*/
  154.         {
  155.             theSound = GetResource('snd ', kSoundID);
  156.             if (theSound != nil) {
  157.                 SndPlay(nil, (SndListHandle)theSound, false);
  158.                 ReleaseResource(theSound);
  159.             }
  160.         }
  161.     }
  162. }
  163.